package com.ym.soso.service.impl;

import com.github.pagehelper.PageInfo;
import com.ym.base.domain.MobileSearchParam;
import com.ym.common.utils.HtmlHandleUtil;
import com.ym.common.utils.StringUtil;
import com.ym.common.utils.page.PageDomain;
import com.ym.core.shiro.ShiroUtil;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.domain.entity.SysDictEntity;
import com.ym.medical.service.ICommonSV;
import com.ym.medical.service.ISysDictSV;
import com.ym.soso.dao.AppHealthPlateDao;
import com.ym.soso.domain.entity.AppHealthPlateEntity;
import com.ym.soso.domain.param.AppHealthPlatePublishParam;
import com.ym.soso.domain.param.AppHealthPlateQueryParam;
import com.ym.soso.domain.param.AppHealthPlateUpdateParam;
import com.ym.soso.domain.vo.AppHealthPlateListMobileVO;
import com.ym.soso.domain.vo.AppHealthPlateListPcVO;
import com.ym.soso.service.IAppHealthPlateSV;
import com.ym.soso.service.search.service.AppHealthIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 健康科普Service业务层处理
 *
 * @author ym
 * @date 2020-08-24
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AppHealthPlateSVImpl extends BaseSVImpl<AppHealthPlateEntity, AppHealthPlateDao> implements IAppHealthPlateSV {

	@Autowired
	private ISysDictSV sysDictSV;

	@Autowired
	private ICommonSV commonSV;

	@Autowired
	private AppHealthIndexService appHealthIndexService;

	/**
	 * 新增健康科普
	 *
	 * @param updateParam 健康科普 新增参数
	 * @return 结果
	 */
	@Override
	public Long insertPC(AppHealthPlateUpdateParam updateParam) {
		AppHealthPlateEntity update = updateParam.covert();
		update.setCreatePerson(ShiroUtil.getLoginId());
		// 最大序号查询
		Map<String, Object> fieldInfo = new HashMap<>();
		fieldInfo.put("parentTag", updateParam.getParentTag());
		fieldInfo.put("tag", updateParam.getTag());
		Long sortNum = commonSV.findMaxSort(AppHealthPlateEntity.class, "sort_num", fieldInfo) + 1;
		update.setSortNum((sortNum.intValue()));
		mapper.insertSelective(update);
		// 索引更新
		searchUpdateIndex(update.getId());
		return update.getId();
	}

	/**
	 * 修改健康科普
	 *
	 * @param update 健康科普 修改参数
	 * @return 结果
	 */
	@Override
	public int updateByIdPC(AppHealthPlateUpdateParam update) {
		AppHealthPlateEntity entity = update.covert();
		int rows = this.updateById(entity);
		// 索引更新
		searchUpdateIndex(entity.getId());
		return rows;
	}


	@Override
	public PageInfo<AppHealthPlateListPcVO> page(AppHealthPlateQueryParam queryParam) {
		PageDomain.startPage();
		PageDomain.orderBy(" publish_time desc ");
		List<AppHealthPlateListPcVO> data = mapper.selectListPC(queryParam);
		PageInfo<AppHealthPlateListPcVO> pageInfo = new PageInfo<>(data);
		Map<String, String> appHealthPlateParentTags = sysDictSV.getDictValuesToMap("AppHealthPlateParentTags");
		List<SysDictEntity> sysDicts = sysDictSV.getDictValues("AppHealthPlateSubTags");
		// 字典值设置
		for (AppHealthPlateListPcVO plate : data) {
			plate.setParentTagName(appHealthPlateParentTags.get(plate.getParentTag()));
			if (StringUtil.isNotBlank(plate.getTag()) && StringUtil.isNotBlank(plate.getParentTag())) {
				for (SysDictEntity sysDict : sysDicts) {
					String parentItemCode = sysDict.getParentItemCode();
					if (parentItemCode.equals(plate.getParentTag()) && plate.getTag().equals(sysDict.getItemCode())) {
						plate.setTagName(sysDict.getItemName());
					}
				}
			}
		}

		return pageInfo;
	}

	@Override
	public PageInfo<AppHealthPlateListMobileVO> pageMobile(int pageNum, int pageSize, AppHealthPlateQueryParam queryParam) {
		PageDomain.startPage(pageNum, pageSize);
		List<Long> ids = appHealthIndexService.searchByKeywordOfIds(MobileSearchParam.build(null, null, null, queryParam.getTitle()));
		if (ids.isEmpty()) {
			return new PageInfo<>();
		}
		queryParam.setIds(ids.toArray(new Long[]{}));
		List<AppHealthPlateListMobileVO> data = mapper.selectListMobile(queryParam);
		PageInfo<AppHealthPlateListMobileVO> pageInfo = new PageInfo<>(data);
		Map<String, String> appHealthPlateParentTags = sysDictSV.getDictValuesToMap("AppHealthPlateParentTags");
		List<SysDictEntity> sysDicts = sysDictSV.getDictValues("AppHealthPlateSubTags");
		// 字典值设置
		for (AppHealthPlateListMobileVO plate : data) {
			plate.setParentTagName(appHealthPlateParentTags.get(plate.getParentTag()));
			if (StringUtil.isNotBlank(plate.getTag()) && StringUtil.isNotBlank(plate.getParentTag())) {
				for (SysDictEntity sysDict : sysDicts) {
					String parentItemCode = sysDict.getParentItemCode();
					if (parentItemCode.equals(plate.getParentTag()) && plate.getTag().equals(sysDict.getItemCode())) {
						plate.setTagName(sysDict.getItemName());
					}
				}
			}
		}
		return pageInfo;
	}

	@Override
	public AppHealthPlateEntity selectByIdCustom(Long id, int width) {
		AppHealthPlateEntity clinicManual = this.selectById(id);
		// 处理图片宽度
		String content = HtmlHandleUtil.resize(clinicManual.getContent(), width);
		clinicManual.setContent(content);
		// 查看次数+1
		readInc(id, "01");
		return clinicManual;
	}

	@Override
	public void readInc(Long id, String type) {
		mapper.readInc(id, type);
	}

	@Override
	public void release(AppHealthPlatePublishParam publishParams) {
		AppHealthPlateEntity appHealthPlate = new AppHealthPlateEntity();
		appHealthPlate.setPublishStatus(publishParams.getState());
		appHealthPlate.setPublishTime(new Date());
		this.updateByIds(appHealthPlate, publishParams.getIds());
	}

	/**
	 * 查询数据并更新
	 */
	private void searchUpdateIndex(Long id) {
		AppHealthPlateEntity entity = this.selectById(id);
		if (entity != null) {
			appHealthIndexService.saveOrUpdate(entity);
		}
	}

}
